package com.share.homestay.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.homestay.dao.HostelOrdersMapper;
import com.share.homestay.entity.HostelOrders;
import com.share.homestay.entity.OrderOperationLogs;
import com.share.homestay.pojo.JsonResult;
import com.share.homestay.service.HostelOrdersService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class HostelOrdersServiceImpl extends ServiceImpl<HostelOrdersMapper, HostelOrders> implements HostelOrdersService {

    @Override
    public JsonResult findAllHostelOrders(HostelOrders hostelOrders, Long pageNum, Long pageSize) {
        return JsonResult.success("成功",this.baseMapper.findAllHostelOrders(hostelOrders,pageNum,pageSize),this.baseMapper.getCount(hostelOrders));
    }

    @Override
    public JsonResult findOrderOperationLogs(String id, Long pageNum, Long pageSize) {
        return JsonResult.success("成功",this.baseMapper.findOrderOperationLogs(id,pageNum,pageSize),this.baseMapper.countOrderOperationLogs(id));
    }

    @Override
    public Long addOrderOperationLog(OrderOperationLogs orderOperationLogs) {
        HostelOrders hostelOrders = new HostelOrders();
        hostelOrders.setId(orderOperationLogs.getId());
        if(orderOperationLogs.getActionContent().equals("入住操作")){
            hostelOrders.setOrderStatus(3);
            this.baseMapper.updateById(hostelOrders);
        }else if (orderOperationLogs.getActionContent().equals("取消订单") || orderOperationLogs.getActionContent().equals("退款操作")){
            hostelOrders.setOrderStatus(6);
            this.baseMapper.updateById(hostelOrders);
        }else if (orderOperationLogs.getActionContent().equals("删除提示")){
            hostelOrders.setOrderStatus(-1);
            this.baseMapper.updateById(hostelOrders);
        }
        if(orderOperationLogs.getMyTime()!=null){
            String[] time = orderOperationLogs.getMyTime().split(" ");
            try {
                hostelOrders.setStartTime(new SimpleDateFormat("yyyy-MM-dd").parse(time[0]));
                hostelOrders.setEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(time[1]));
                hostelOrders.setUpdateTime(new Date());
                hostelOrders.setUpdateBy(orderOperationLogs.getActionName());
                hostelOrders.setRemark(orderOperationLogs.getActionContent());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            this.baseMapper.updateById(hostelOrders);
        }
        return this.baseMapper.addOrderOperationLog(orderOperationLogs);
    }
}
